package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.OrderDao;
import cn.sumpu.app.guoguojie.domain.Order;
import cn.sumpu.app.guoguojie.domain.OrderProduct;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class OrderDaoImpl extends SqlSessionDaoSupport implements OrderDao {
	private static Logger logger = LoggerFactory.getLogger(OrderDaoImpl.class);

	@Override
	public void insertOrder(Order order) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.OrderMapper.insertOrder",
							order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertOrder database failure.", e);
			}
			throw new AppBusinessException("insertOrder database failure.", e);
		}

	}

	@Override
	public void insertOrderProduct(OrderProduct orderProduct)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.OrderMapper.insertOrderProduct",
							orderProduct);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertOrder database failure.", e);
			}
			throw new AppBusinessException("insertOrder database failure.", e);
		}

	}

	@SuppressWarnings("unchecked")
	public List<Order> getOrderList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.OrderMapper.getOrderList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderList failure.", e);
			}
			throw new AppBusinessException("getOrderList failure.", e);
		}
	}

	@Override
	public int getOrderCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.OrderMapper.getOrderCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderCount failure.", e);
			}
			throw new AppBusinessException(
					"getOrderCount failure.", e);
		}
	}

	@Override
	public Order getOrderById(String orderId) throws AppBusinessException {
		try {
			return (Order) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.OrderMapper.getOrderById",
							orderId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOrderById failure.", e);
			}
			throw new AppBusinessException("getOrderById failure.", e);
		}
	}

	@Override
	public void updateOrder(Order order) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.OrderMapper.updateOrder",
					order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update order failure.", e);
			}
			throw new AppBusinessException("update order failure.", e);
		}
		
	}

	@Override
	public void updateOrderProduct(OrderProduct orderProduct)
			throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.OrderMapper.updateOrderProduct",
					orderProduct);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update OrderProduct failure.", e);
			}
			throw new AppBusinessException("update OrderProduct failure.", e);
		}
		
	}

	@Override
	public void deleteOrder(String orderId) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.OrderMapper.deleteOrder",orderId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteOrder failure.", e);
			}
			throw new AppBusinessException("deleteOrder failure.", e);
		}
		
	}

	@Override
	public void deleteOrderProduct(String orderId) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.OrderMapper.deleteOrderProduct",orderId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteOrderProduct failure.", e);
			}
			throw new AppBusinessException("deleteOrderProduct failure.", e);
		}
		
	}

	@Override
	public int orderNoReadCount(String company) throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.OrderMapper.getCompanyNoReadOrderCount",
							company);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("orderNoReadCount failure.", e);
			}
			throw new AppBusinessException(
					"getOrderCount failure.", e);
		}
	}

	@Override
	public void companyUpdateOrder(Order order) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.OrderMapper.companyUpdateOrder",
					order);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update order failure.", e);
			}
			throw new AppBusinessException("update order failure.", e);
		}
		
	}

}
